package whu.hami.tea.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import whu.hami.tea.R;
import whu.hami.tea.fragment.Register_1;
import whu.hami.tea.fragment.Register_2;
import whu.hami.tea.fragment.Register_3;
import whu.hami.tea.view.TitleBar;

/**
 * Created by pendragon on 17-5-27.
 */

public class RegisterActivity extends AppCompatActivity {
    private TitleBar titleBar;
    public Handler myHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        titleBar = (TitleBar) findViewById(R.id.titleBar);
        titleBar.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getFragmentManager().getBackStackEntryCount() != 0)
                    getFragmentManager().popBackStack();
                else {
                    startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                    finish();
                }
            }
        });

        myHandler = new MyHandler(getFragmentManager(), titleBar, this);
        myHandler.sendEmptyMessage(0);
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() != 1){
            getFragmentManager().popBackStack();
        } else {
            startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
            finish();
        }
    }

    static class MyHandler extends Handler {
        private FragmentManager fm;
        private TitleBar titleBar;
        private RegisterActivity registerActivity;

        MyHandler(FragmentManager fm, TitleBar titleBar, RegisterActivity registerActivity){
            this.fm = fm;
            this.titleBar = titleBar;
            this.registerActivity = registerActivity;
        }

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                titleBar.setTitle("手机号注册");

                FragmentTransaction transaction = fm.beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(R.id.fm_container, new Register_1());
                transaction.commit();
            } else if (msg.what == 1){
                titleBar.setTitle("手机号验证");

                FragmentTransaction transaction = fm.beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(R.id.fm_container, new Register_2());
                transaction.commit();
            } else if (msg.what == 2){
                titleBar.setTitle("完善个人信息");

                FragmentTransaction transaction = fm.beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(R.id.fm_container, new Register_3());
                transaction.commit();
            } else if (msg.what == 3){
                Intent intent = new Intent(registerActivity, MainActivity.class);
                registerActivity.startActivity(intent);
                registerActivity.finish();
            }
        }
    }
}
